package it.polimi.swKnights.SWIMv2.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class GuestOnlyFilter
 */
public class GuestOnlyFilter extends SwimFilter implements Filter
{

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException
	{

		HttpServletRequest req = (HttpServletRequest) request;
		HttpSession session = req.getSession();

		if (session.getAttribute("userName") != null
				&& session.getAttribute("password") != null)
		{
			HttpServletResponse resp = (HttpServletResponse) response;
			resp.sendRedirect("userHome");
		}
		else
		{
			this.setupSessionType(session, "logged", "guest");
			this.setupSessionType(session, "admin", "guest");
			chain.doFilter(request, response);
		}
	}

}
